package viewcomponents
{
	import flash.display.GradientType;
	import flash.display.Graphics;
	import flash.events.MouseEvent;
	import flash.geom.Matrix;
	
	import fui.FUIControllers.FUIMenuEvent;
	
	import mx.containers.Canvas;
	import mx.containers.HBox;
	import mx.controls.VRule;
	
	[Event(name="menuClick", type="fui.FUIControllers.FUIMenuEvent")]
	
	public class MenuPanel extends Canvas
	{
		private var hbox:HBox;
		private var _options:Array;
		
		
		public function MenuPanel(options:Array)
		{
			hbox = new HBox();
			hbox.percentWidth = 100;
			hbox.percentHeight = 100;
			hbox.setStyle("horizontalGap", 0);
			addChild(hbox);
			_options = options;
			buildButtons(_options);
			
		}
		
		//add button to menu
		public function addButton(button:MenuButton):void{
			hbox.addChild(button);
			var vrule:VRule = new VRule();
			vrule.percentHeight = 100;
			hbox.addChild(vrule);
		}
		
		//remove menu button
		public function removeButton(button:MenuButton):void{
			var ind:int = hbox.getChildIndex(button);
			if( ind >= 0){
				hbox.removeChildAt(hbox.getChildIndex(button) + 1);
				hbox.removeChild(button);
			}
		}
		
		//build menu buttons
		public function buildButtons(arr:Array):void{
			if(hbox.getChildren().length > 0){
				hbox.removeAllChildren();
			}
			for each(var opt:Object in arr){
				var but:MenuButton = new MenuButton(opt as String);
				//var but:MenuButton = new MenuButton(opt["caption"]
				but.percentWidth = 20;
				hbox.addChild(but);
				var vrule:VRule = new VRule();
				vrule.percentHeight = 100;
				hbox.addChild(vrule);
				but.addEventListener(MouseEvent.CLICK, clickOn);
				
			}
		}
		
		//click on one of the buttons
		private function clickOn(event:MouseEvent):void{
			var fuievent:FUIMenuEvent = new FUIMenuEvent(FUIMenuEvent.MENU_CLICK);
			fuievent.content = event.target.caption;
			dispatchEvent(fuievent);
		}
		
		override protected function updateDisplayList(w:Number, h:Number) :void
        {
            super.updateDisplayList (w, h);
             var _g:Graphics = graphics;
             var _m:Matrix  = new Matrix();
             _m.createGradientBox (w, h, Math.PI*0.5);
            
             _g.clear ();
            
            // so, draw !
             _g.beginGradientFill (GradientType.LINEAR, [0xa1f3b0, 0xcaf290], [1, 1], [0, 255], _m);
             _g.drawRect(0, 0, w, h);
             _g.endFill();

        } 

	}
}